package hu.szyrt.bug.adapters;

import hu.szyrt.bug.PlaceActivity;
import hu.szyrt.bug.utils.Assets;
import hu.szyrt.bug.views.ResizableImageView;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

public class HighlightsAdapter extends PagerAdapter {
	
	private Assets assets;
	private String[] names;
	public HighlightsAdapter(Assets assets, String[] names) {
		this.assets = assets;
		this.names = names;
	}
	
    @Override
    public void destroyItem(View collection, int position, Object o) {
        ImageView image = (ImageView)o;
        if(image != null){
        	if(image.getDrawable() != null) ((BitmapDrawable)image.getDrawable()).getBitmap().recycle();
        	((ViewPager) collection).removeView(image);
        	image = null;
        }
    }

    @Override
    public void finishUpdate(View arg0) {}
    @Override
    public int getCount() { return names.length; }
    @Override
    public Object instantiateItem(final View context, final int position) {
        ResizableImageView imageView = new ResizableImageView(assets.getContext());	    	
        imageView.setImageBitmap(assets.loadHighlights(names[position]));
        imageView.setOnGridClickListener(new ResizableImageView.OnGridClickListener() {
			public void onClick(int rowIndex, int columnIndex) {
				// (Position * 2 + 1) + (10 * rowIndex) + columnIndex;
				int hihghlights_id = (position * 2 + 1) + (10 * rowIndex) + columnIndex;				
				PlaceActivity.showPlaceBy(context.getContext(), PlaceActivity.HIGHLIGHTS_ID, hihghlights_id);
			}
		});
        ((ViewPager) context).addView(imageView);
        return imageView;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView)object);
    }
    
}
